/**
 * # # THIS FILE IS PART OF ripap PROJECT
 * # #
 * # RegexUtils.java - 正则表达式验证工具类
 * #
 * # THIS PROGRAM IS OPENSOURCE SOFTWARE, IS LICENSED UNDER BSD
 * # YOU SHOULD HAVE RECEIVED A COPY OF WTFPL LICENSE
 * #
 * # Copyright (c) 2018 Ting Fan
 */
package com.cloudkinto.common.utils.regex;


import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * @program     : ripap
 * @description : 正则表达式验证工具类
 * @author      : Ting Fan
 * @company     : 苏州
 * @create      : 2019-05-18 17:45
 **/
public class RegexUtils {

    /**
     * 编码正则表达验证方法  编码只能由数字、字母、以及- _组成
     * @param sourceStr
     * @return
     */
    public static boolean codeRegex(String sourceStr) {
        boolean resultBoolean = true;
//        String pattern1 = "^\\w+$";
//        String pattern2 = "^[A-Za-z0-9-]*$";

        String pattern1 = "^[\\u4e00-\\u9fa5A-Za-z0-9-\\_]+$";
//        String pattern2 = "^[A-Za-z0-9-]*$";
//        resultBoolean = Pattern.matches(pattern2, sourceStr) || Pattern.matches(pattern1, sourceStr);
        resultBoolean = Pattern.matches(pattern1, sourceStr);
        return resultBoolean;
    }

    public static boolean numberRegex(String sourceStr) {
        if (StringUtils.isBlank(sourceStr)) {
            return true;
        }
        boolean resultBoolean = true;
        String pattern = "^\\d+$";
        resultBoolean = Pattern.matches(pattern, sourceStr);
        return resultBoolean;
    }

    public static boolean doubleNumberRegex(String sourceStr) {
        boolean resultBoolean = false;
        String pattern = "^(\\-|\\+)?\\d+(\\.\\d+)?$";
        resultBoolean = Pattern.matches(pattern, sourceStr);
        return resultBoolean;
    }

    public static boolean urlVerification(String sourceStr) {
        boolean resultBoolean = true;
        String pattern = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
        resultBoolean = Pattern.matches(pattern, sourceStr);
        return resultBoolean;
    }
    public void find(String searchStr){
        String pattern = "[ ]*";
        String regex = "(.{1})";
        searchStr = searchStr.replaceAll(regex, "$1"+pattern);
        System.out.println(searchStr);
    }

    public static void main(String[] args) {
//        System.out.println(numberRegex("123121223"));
//        System.out.println(numberRegex("123ddfdfd"));
//        System.out.println(codeRegex("_"));
//        System.out.println(codeRegex("12d_ssd-"));
//        System.out.println(codeRegex("_12dssd--"));
//        System.out.println(codeRegex("12dssd-?-"));


        System.out.println(urlVerification("https[//localhost:8080"));
    }
}
